package com.xinke.module.system.manager.oauth2;

import com.xinke.module.system.model.entity.oauth2.OAuth2AccessTokenDO;
import com.xinke.module.system.model.entity.oauth2.OAuth2ClientDO;
import com.xinke.module.system.model.entity.oauth2.OAuth2RefreshTokenDO;
import com.xinke.module.system.service.oauth2.OAuth2AccessTokenService;
import com.xinke.module.system.service.oauth2.OAuth2ClientService;
import com.xinke.module.system.service.oauth2.OAuth2RefreshTokenService;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author yzx
 * @CreateTime 2025/3/21 0:29
 * @Version 1.0
 * @Description:
 */
@Component
public class OAuth2Manager {

    @Resource
    private OAuth2ClientService clientService;

    @Resource
    private OAuth2RefreshTokenService refreshTokenService;

    @Resource
    private OAuth2AccessTokenService accessTokenService;

    @Transactional(rollbackFor = Exception.class)
    public OAuth2AccessTokenDO createAccessToken(Long userId, Integer userType, String clientId, List<String> scopes) {
        OAuth2ClientDO clientDO = clientService.validOAuthClientFromCache(clientId);
        // 创建刷新令牌
        OAuth2RefreshTokenDO refreshTokenDO = refreshTokenService.createOAuth2RefreshToken(userId, userType, clientDO, scopes);
        // 创建访问令牌
        return accessTokenService.createOAuth2AccessToken(refreshTokenDO, clientDO);
    }
}
